package com.kust.smarhome.background.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kust.smarhome.background.common.dto.EvaluateDto;
import com.kust.smarhome.background.common.dto.EvaluateImageDto;
import com.kust.smarhome.background.common.lang.Result;
import com.kust.smarhome.background.model.Evaluate;
import com.kust.smarhome.background.service.IEvaluateService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author iAuzre
 * @since 2021-07-31
 */
@RestController
@RequestMapping("/evaluate")
public class EvaluateController extends BaseController {

    @Resource
    private IEvaluateService evaluateService;

    @GetMapping("/list")
    public Result list(String orderFromId, Principal principal) {

        if (getUserPower(principal.getName()))
            return Result.fail(401, "权限不足", null);

        Page<EvaluateDto> page = evaluateService.getEvaluateInfo(getPage(), orderFromId);

        return Result.succ(page);

    }

    @GetMapping("/{id}/setFeatured")
    public Result setFeatured(@PathVariable Integer id, Principal principal) {

        if (getUserPower(principal.getName()))
            return Result.fail(401, "权限不足", null);

        Evaluate evaluate = evaluateService.getById(id);
        if (evaluate.getState() == 1)
            return Result.fail("操作失败");
        evaluate.setState(1);
        if (!evaluateService.updateById(evaluate))
            return Result.fail("系统错误");

        return Result.succ(null);

    }

    @GetMapping("/{id}/setOffFeatured")
    public Result setOffFeatured(@PathVariable Integer id, Principal principal) {

        if (getUserPower(principal.getName()))
            return Result.fail(401, "权限不足", null);

        Evaluate evaluate = evaluateService.getById(id);
        if (evaluate.getState() == 0)
            return Result.fail("操作失败");
        evaluate.setState(0);
        if (!evaluateService.updateById(evaluate))
            return Result.fail("系统错误");

        return Result.succ(null);

    }

    @PostMapping("/delete")
    @Transactional
    public Result delete(@RequestBody Integer[] ids, Principal principal) {

        if (getUserPower(principal.getName()))
            return Result.fail(401, "权限不足", null);

        for (int id : ids) {
            evaluateService.removeById(id);
        }

        return Result.succ(null);
    }

    @GetMapping("/getImage/{id}")
    public Result getImage(@PathVariable Integer id, Principal principal) {

        if (getUserPower(principal.getName()))
            return Result.fail(401, "权限不足", null);

        Evaluate evaluate = evaluateService.getById(id);
        if (evaluate.getImgurl1() == null)
            return Result.succ(201, "该评价没有照片", null);
        EvaluateImageDto evaluateImageDto = new EvaluateImageDto();
        evaluateImageDto.setImgurl1(evaluate.getImgurl1())
                .setImgurl2(evaluate.getImgurl2())
                .setImgurl3(evaluate.getImgurl3())
                .setImgurl4(evaluate.getImgurl4());

        return Result.succ(evaluateImageDto);

    }

}
